package mob.dev.blpr;

//import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
//import android.widget.CheckBox;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
//import android.widget.TextView;
//import android.widget.Button;
//import android.view.View;
import android.content.Intent;
//import java.util.Date;
//import com.google.api.client.util.DateTime;
//import java.text.SimpleDateFormat;

public class newTaskActivity extends Activity{
	
	private static final int CANCEL = 0;
	private static final int SAVE = 1;
	private static final int TIME_DIALOG_ID = 0;
	private static final int DATE_DIALOG_ID = 1;
	private EditText task;
	private EditText notes;
	private CheckBox allDay;
	private Time dueDateTime;
	private Button dueDate;
	private Button dueTime;
	private String taskId;
	//private boolean needReminder;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        
        //reminder = (CheckBox)findViewById(R.id.reminder);
        //needReminder = false;
        
        task = (EditText)findViewById(R.id.editTask);
        notes = (EditText)findViewById(R.id.editNotes);
        allDay = (CheckBox)findViewById(R.id.checkBox1);
        allDay.setOnClickListener(new View.OnClickListener(){
        	
        	 @Override
             public void onClick(View v) {
                 if(((CheckBox) v).isChecked())
                     Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show();
                 else
                     Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_SHORT).show();
                 
                 onAllDayClick();
             }
        	
        	
        });
       
        
        dueDate = (Button)findViewById(R.id.dateButton);
        
        //fromDatePicker.setText(dateNow);
        dueDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        dueTime = (Button) findViewById(R.id.timeButton);
        //fromTimePicker.setText(timeNow);
        dueTime.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });
        
        
      
        
        Bundle extras = getIntent().getExtras();
        if(extras != null){
        	taskId = extras.getString("Id");
        	task.setText(extras.getString("Title"));
        	notes.setText(extras.getString("Notes"));
        	//TODO figure out how to split the DateTime string passed in on the intent
        	String dueInput = extras.getString("DueDate");
        	if(!dueInput.equals("")){
        		dueDateTime = new Time();
        		Boolean tz = dueDateTime.parse3339(dueInput);
        		if (tz){
        			allDay.setChecked(false);
        			dueTime.setText(dueDateTime.format("%I:%M %p"));
        		}else{
        			allDay.setChecked(true);
        			dueTime.setText("All Day");
        		}
        	}else{
        		dueDateTime = new Time();
        		dueDateTime.setToNow();
        		allDay.setChecked(false);
        		dueTime.setText(dueDateTime.format("%I:%M %p"));
        	}
        	dueDate.setText(dueDateTime.format("%D"));
        	
        	//TODO Android has class Preference that has function getPreference("timezone") that we can use to default TZ.
        	
        }else{
        	dueDateTime = new Time();
    		dueDateTime.setToNow();
    		//TODO Same as last to-do we need to figure out the time zone issue
    		dueTime.setText(dueDateTime.format("%I:%M %p"));
    		dueDate.setText(dueDateTime.format("%D"));
        }
        
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	        case TIME_DIALOG_ID:
	            return new TimePickerDialog(this,timeSetListener, dueDateTime.hour, dueDateTime.minute, false);
	        case DATE_DIALOG_ID:
	            return new DatePickerDialog(this,dateSetListener, dueDateTime.year, dueDateTime.month, dueDateTime.monthDay);
	                        
	    }
	    return null;
	}
	
	 private TimePickerDialog.OnTimeSetListener timeSetListener =
		        new TimePickerDialog.OnTimeSetListener() {
		            
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						dueDateTime.hour= hourOfDay;
						dueDateTime.minute = minute;
						allDay.setChecked(false);
						dueTime.setText(dueDateTime.format("%I:%M %p"));
					}
		        };
	private DatePickerDialog.OnDateSetListener dateSetListener = 
			new DatePickerDialog.OnDateSetListener() {

				@Override
				public void onDateSet(DatePicker view, int year,
						int monthOfYear, int dayOfMonth) {
					dueDateTime.year = year;
					dueDateTime.month = monthOfYear;
					dueDateTime.monthDay = dayOfMonth;
					dueDate.setText(dueDateTime.format("%D"));
					
				}
		
			};
	
	public void onSaveClick(View v){
		Intent enteredTask = new Intent();
		//TODO add reminder checkbox back
		//add fields that will be put into new google task to the intent
		enteredTask.putExtra("Id", taskId);
		enteredTask.putExtra("Title", task.getText().toString());
		enteredTask.putExtra("Notes", notes.getText().toString());
		//TODO add checkbox that turns on all day mode and uses a boolean that is passed to method on next line
		enteredTask.putExtra("DueDate", dueDateTime.format3339(allDay.isChecked()));
		//this tells the main activity that we are finished creating a new task
		setResult(SAVE, enteredTask);
		finish();
	}
	
	public void onCancelClick(View v){
		
		Intent cancelledTask = new Intent();
		setResult(CANCEL, cancelledTask);
		finish();
	}
	
	public void onAllDayClick(){
		Time nowTime = null;
		if (allDay.isChecked()){
			dueTime.setText("All Day");
			dueDateTime.hour = 0;
			dueDateTime.minute = 0;
			dueDateTime.second = 0;
		}
		else{
			nowTime = new Time();
			dueDateTime.hour = nowTime.hour;
			dueDateTime.minute = nowTime.minute;
			dueTime.setText(dueDateTime.format("%I:%M %p"));
		}
			
	}
	
	@Override
	public void finish(){
		super.finish();
	}
	
	
}
